<!--智慧消防V1.0-upTel页面-->
<template>
    <div class="app-container">
        <el-row :gutter="10">
            <el-col :xs="24" :sm="18" :md="14" :lg="10" :xl="8">
                <div style="text-align: center;margin-bottom: 10px">修改手机号</div>
                <el-form :model="telForm" ref="telForm" :rules="telFormRules" label-width="80px">
                    <el-form-item label="手机号" prop="mobile">
                        <el-input type="text" maxlength="11" suffix-icon="el-icon-mobile-phone" v-model="telForm.mobile" placeholder="请输入手机号"></el-input>
                    </el-form-item>
                    <el-form-item label="验证码" prop="code">
                        <div class="verify-wrapper">
                            <el-input type="text" style="width: calc(100% - 108px)" maxlength="6" suffix-icon="el-icon-lock" placeholder="验证码" v-model="telForm.code"/>
                            <el-button type="primary" style="margin-left: 10px;width: 98px" :disabled="disabled" @click="getCode">{{codeBtn}}</el-button>
                        </div>
                    </el-form-item>
                </el-form>
                <div  style="text-align: center" class="dialog-footer">
                    <el-button @click="cancelUp">取 消</el-button>
                    <el-button :loading="addLoading" type="primary" @click="updateMobile">修 改</el-button>
                </div>
            </el-col>
        </el-row>
    </div>
</template>

<script>
import {sendSms} from "@/api/common";
import {updateMobile, updatePassword} from "@/api/enterprise";

export default {
    name: "upMobile",
    data(){
        let checkPhone = (rule, value, callback) => {
            const reg = /^0?(13[0-9]|15[012356789]|18[0-9]|14[578]|16[6]|17[035768]|19[19])[0-9]{8}$/
            if ( reg.test(value) ) {
                return callback();
            }
            callback('请输入合法的手机号');
        };
        return{
            telForm:{},
            telFormRules:{
                mobile: [{validator: checkPhone, required: true, trigger: 'blur' } ],
                code:[{required: true, message: '请输入验证码', trigger: 'blur' } ]
            },
            codeBtn:'获取验证码',
            disabled:false,
            addLoading:false
        }
    },
    methods:{
        //获取验证码
        //获取验证码 并只验证手机号 是否正确
        getCode(){
            this.$refs['telForm'].validateField('mobile', (err) =>{
                if(err){
                    console.log('未通过')
                    return;
                }else{
                    console.log('已通过')
                    sendSms({mobile: this.telForm.mobile,type:1}).then(res=>{
                        this.tackBtn();   //验证码倒数60秒
                        this.$notify.success({
                            title: '成功',
                            message: "验证码发送成功"
                        })

                    }).catch(error=>{
                        this.$notify.error({
                            title: '失败',
                            message: "验证码发送失败"
                        })
                    })
                }
            })
        },
        tackBtn(){       //验证码倒数60秒
            let time = 60;
            let timer = setInterval(() => {
                if(time == 0){
                    clearInterval(timer);
                    this.codeBtn = '获取验证码';
                    this.disabled = false;
                }else{
                    this.disabled = true;
                    this.codeBtn = time + '秒后重试';
                    time--;
                }
            }, 1000);
        },
        cancelUp(){
            window.history.back();
        },
        updateMobile(){
            this.addLoading = true
            this.$refs['telForm'].validate(valid => {
                if (valid){
                    updateMobile(this.telForm).then(res=>{
                        this.addLoading = false
                        this.$notify.success({
                            title: '成功',
                            message: "更新手机号成功"
                        })
                        this.cancelUp()
                    }).catch(res=>{
                        this.addLoading = false
                        this.$notify.error({
                            title: '失败',
                            message: res.msg
                        })
                    })
                }else {
                    this.addLoading = false
                    console.log("表单验证不通过")
                }
            })
        }
    }
}
</script>

<style scoped>

</style>
